package ink.onei.framework.web.service;

import ink.onei.common.constant.CacheConstants;
import ink.onei.common.constant.Constants;
import ink.onei.common.core.domain.model.LoginUser;
import ink.onei.common.core.redis.RedisCache;
import ink.onei.common.exception.user.CaptchaException;
import ink.onei.common.exception.user.CaptchaExpireException;
import ink.onei.common.utils.MessageUtils;
import ink.onei.common.utils.StringUtils;
import ink.onei.framework.manager.AsyncManager;
import ink.onei.framework.manager.factory.AsyncFactory;
import ink.onei.framework.security.context.AuthenticationContextHolder;
import ink.onei.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

/**
 * @Author: nekotako
 * @Description: TODO
 * @Date: 31/01/2024 11:11 Wednesday
 */

@Service
public class SellerLoginService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    @Qualifier("sellerAuthenticationManager")
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysConfigService configService;

    public String login(String username, String password, String code, String uuid) {
        // 验证码校验
        validateCaptcha(username, code, uuid);
        // 用户验证
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
        AuthenticationContextHolder.setContext(authenticationToken);
        // 用户验证
        Authentication authentication = null;
        authentication = authenticationManager.authenticate(authenticationToken);
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // 生成token
        return tokenService.createToken(loginUser);
    }


    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public void validateCaptcha(String username, String code, String uuid) {
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        if (captchaEnabled) {
            String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
            String captcha = redisCache.getCacheObject(verifyKey);
            redisCache.deleteObject(verifyKey);
            if (captcha == null) {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
                throw new CaptchaExpireException();
            }
            if (!code.equalsIgnoreCase(captcha)) {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
                throw new CaptchaException();
            }
        }
    }
}
